#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          to be called from test21.34
#          Tests if the Grpwall limit is enforced
############################################################################
# Copyright (C) 2015 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################

proc inc21_34_2 { qostest } {
	global salloc srun acct bin_sleep grpwall_num number exit_code

	send_user "\nStarting Grpwall test (Within: inc21.34.2)\n\n"
	set job_id1 0
	set job_id2 0
	set timeout 120

	# Since wall is a decayed variable, reset it to make sure the test
	# gets exactly what we would expect.
	reset_qos_usage "" $qostest

	spawn $salloc --account=$acct -N2 -t1 $srun $bin_sleep [expr $grpwall_num * 60 + 1]
	expect {
		-re "Granted job allocation ($number)" {
			set job_id1 $expect_out(1,string)
			send_user "\nJob $job_id1 has been submitted. (Within: inc21.34.2)\n"
		}
		-re "Job violates accounting/QOS policy" {
			send_user "\nFAILURE: This job should not be pending, but is. (Within: inc21.34.2)\n"
			set exit_code 1
		}
		-re "error" {
			send_user "\nFAILURE: Job allocation should not have failed. (Within: inc21.34.2)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc not responding. (Within: inc21.30.2)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$job_id1 == 0} {
		send_user "\nFAILURE: job was not submitted\n"
		set exit_code 1
	} else {
		# Wait for job 1 to finish so that we can test wall time limit
		wait_for_job $job_id1 DONE
	}

	set match 0
	spawn $salloc -I --account=$acct -N2 -t1 $srun $bin_sleep 10
	expect {
		-re "Job violates accounting/QOS policy" {
			send_user "\nJob is waiting for resources. This is exected. (Within: inc21.34.2)\n"
			set match 1
		}
		-re "Granted job allocation ($number)" {
			set job_id2 $expect_out(1,string)
			send_user "\nFAILURE: Job $job_id2 should be waiting for resources, but is not. (Within: inc21.34.2)\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: salloc is not responding (Within: inc21.34.2)\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$match != 1} {
		send_user "\nFAILURE: job was submitted when it should not have been\n"
		set exit_code 1
	}

	# Reset QoS usage
	reset_qos_usage "" $qostest

	# Cancel job
	if {$exit_code} {
		cancel_job $job_id2
		cancel_job $job_id1
	} else {
		cancel_job $job_id2
	}
}
